

	<title>角色管理</title>

	<div class="layui-card layadmin-header">
		<div class="layui-breadcrumb" lay-filter="breadcrumb">
			<a lay-href="">主页</a>
			<a><cite>权限中心</cite></a>
			<a><cite>角色管理</cite></a>
		</div>
	</div>
	
	<div class="site-text" style="margin: 5%; display: none" id="window"  target="test123">	
		<form class="layui-form" method="post" >
			
			
		  <div class="layui-form-item">
		    <label class="layui-form-label">角色名称</label>
		    <div class="layui-input-block">
		      <input type="text" id="roleNm" name="roleNm" lay-verify="title" autocomplete="off" placeholder="请输入角色名称" class="layui-input">
		    </div>
		  </div>
		  <div class="layui-form-item">
		    <label class="layui-form-label">描述</label>
		    <div class="layui-input-block">
		      <textarea id="roleDesc" name="roleDesc" lay-verify="roleDesc" autocomplete="off" placeholder="请输入描述" class="layui-input"></textarea>
		    </div>
		  </div>
		  <div class="layui-form-item">
		    <label class="layui-form-label">学校名称</label>
		    <div class="layui-input-block">
		      <select name="school" lay-verify="required" lay-search="" lay-reqtext="请选择所在院校" id="school">
			  </select>
			</div>
		  </div>
		  
		 
		  <div class="layui-form-item">
		    <div class="layui-input-block">
		      <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
		    </div>
		  </div>
		</form>
	</div>
	
	
	<div class="site-text1" style="margin: 5%; display: none" id="window1"  target="test1">
		<form class="layui-form" method="post" >
			<div class="layui-form-item" >
	
			  
			    <input type = "hidden"  id="roleId" name="roleId" lay-verify="Id" autocomplete="off" placeholder="请输入角色名称" class="layui-input">
			  
			</div>
		  <div class="layui-form-item">
		    <label class="layui-form-label">角色名称</label>
		    <div class="layui-input-block">
		      <input type="text" id="roleNm1" name="roleNm1" lay-verify="title1" autocomplete="off" placeholder="请输入角色名称" class="layui-input"></input>
		    </div>
		  </div>
		  <div class="layui-form-item">
		    <label class="layui-form-label">描述</label>
		    <div class="layui-input-block">
		      <textarea id="roleDesc1" name="roleDesc1" lay-verify="roleDesc1" autocomplete="off" placeholder="请输入描述" class="layui-input"></textarea>
		    </div>
		  </div>
		  <div class="layui-form-item">
		    <label class="layui-form-label">学校名称</label>
		    <div class="layui-input-block">
		      <select name="school1" lay-verify="required" lay-search="" lay-reqtext="请选择所在院校" id="school1">
			  </select>
			</div>
		  </div>
		  
		 
		  <div class="layui-form-item">
		    <div class="layui-input-block">
		      <button class="layui-btn" lay-submit="" lay-filter="demo2">提交修改</button>
		    </div>
		  </div>
		</form>
	</div>
	
	
	
	
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-header">角色管理</div>
          <div class="layui-card-body">
            <table class="layui-hide" id="test-table-toolbar" lay-filter="test-table-toolbar"></table>
            
            <script type="text/html" id="test-table-toolbar-toolbarDemo">
              <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="insertCheckData">添加角色</button>
              </div>
            </script>
             
            <script type="text/html" id="test-table-toolbar-barDemo">
              <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
				{{#  if(d.roleStatic == 1){ }}
				<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="no">停用</a> 
				{{# }if(d.roleStatic == 0) { }}
				<a class="layui-btn layui-btn-normal layui-btn-xs yes" lay-event="yes">启用</a>
				{{#  } }}
              <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <script>
	  
	 
	  
	 
	  	
	  
	  
	  
	  
	  
	  
  layui.use(['admin', 'table'], function(){
    var admin = layui.admin
    ,table = layui.table;
	
	
    table.render({
		elem: '#test-table-toolbar'
		,url: path + '/authority/Role/selectAll'
		,toolbar: '#test-table-toolbar-toolbarDemo'
		,title: '角色数据表'
		,cols: [[
    //    {type: 'checkbox', fixed: 'left'}
			{field:'roleId', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, hide:true}
			,{field:'roleNm', title:'角色名称', width:120}
			,{field:'roleDesc', title:'描述', width:300}
			,{field:'schNm', title:'院校名称', width:120}
			,{field:'roleStatic', title:'角色状态', width:120, sort: true, templet: function(d){    //动态显示数据
				if(d.roleStatic == 1)
				{
					return '<span style="color:#0000FF;">' + '有效' + '</span>'
				}
				else
				{
					return '<span style="color:red;">' + '无效' + '</span>'
				}
			}}
			,{field:'addTime', title:'数据添加时间', width:150}
			,{field:'addUserId', title:'数据添加人', width:120}
			,{fixed: 'right', title:'操作', toolbar: '#test-table-toolbar-barDemo', width:300}
		]]
		,request: {
			pageName: 'pageCurrent',  //当前页数
			limitName: 'pageSize'     //每页显示数据笔数
		}
		,limit: 5
		,limits: [5,10,15]
		,page: true
    });
    addoption()
	addoption1()
    //头工具栏事件
    table.on('toolbar(test-table-toolbar)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case 'insertCheckData':
			layui.use(['layer','form'], function(){
				layer.open({
					title: '添加角色'
					,skin: 'layui-layer-molv'
					,area: ['600px', '300px']
					,shadeClose: true
					,type:1
					,shade: 0 //遮罩透明度
					,content: $("#window")
				});
			})
        break;
      };
    });
    
    //监听行工具事件
    table.on('tool(test-table-toolbar)', function(obj){
		var data = obj.data;
		if(obj.event === 'del'){
			layer.confirm('确定删除么', function(index){
			 
			 
			  $.post({
				url:path + "/authority/Role/deleteRole"
				,data:{
					roleId:data.roleId,
					_method:'delete'
				}
				,success:function(res){
					if(res.code == 0){
						layer.msg("删除成功");
						layer.close(index);
						window.location.reload();
					}
				}
			  })
		
			});
			
		} else if(obj.event === 'edit'){
			showRole(data);
			layer.open({
				title: '修改角色'
				,area: ['600px', '300px']
				,skin: 'layui-layer-molv'
				,shadeClose: true
				,type:1
				,shade: 0 //遮罩透明度
				,content: $("#window1")
				,done:addoption1()
			});
		}else if(obj.event === 'yes'){
			$.post({
				url : path + "/authority/Role/updateRoleStatic"
				,data:{
					roleId:data.roleId,
					roleStatic:1,
					_method:'put'
				}
				,success:function(res){
					if(res.code == 0){
						layer.msg("启用成功");
						window.location.reload();
					}
				}
				
			})
		}else if(obj.event === 'no'){
			
			$.post({
				url : path + "/authority/Role/updateRoleStatic"
				,data : {
					roleId:data.roleId,
					roleStatic:0,
					_method:'put'
				}
			    ,success:function(res){
					if(res.code == 0){
						layer.msg("停用成功");
						window.location.reload();
					}
				}
			})
		}
	});
	
	
	function addoption(){
		
		
		$.get({
			url : path + '/authority/School/selectAllSch',
			// headers: { //通过 request 头传递
			//     access_token: layui.data('layuiAdminTest').access_token
			// },
			success:function(res){
				if(res.code == 0){
					var content = "<option value =''"+">请选择院校</option>"
					for(var i=0;i<res.data.length;i++){
						var school = res.data[i];
						content += "<option value='"+school.schId+"'>"+school.schNm+"</option>"
						
					}
					layui.form.render("select"); 
					$("#school").html(content);
				}
			}
		})
	}
	
	
	function addoption1(){
		$.get({
			url : path + '/authority/School/selectAllSch',
			// headers: { //通过 request 头传递
			//     access_token: layui.data('layuiAdminTest').access_token
			// },
			success:function(res){
				if(res.code == 0){
					var content = "<option value =''"+">请选择院校</option>"
					for(var i=0;i<res.data.length;i++){
						var school = res.data[i];
						content += "<option value='"+school.schId+"'>"+school.schNm+"</option>"
						
					}
					layui.form.render("select"); 
					$("#school1").html(content);
				}
			}
		})
	}
	
	function showRole(data){
		$.get({
			url : path + "/authority/Role/selectRoleByRoleId",
			data : {
				roleId:data.roleId
			},
			success:function(res){
				if(res.code == 0){
					var mRole = res.data
					$("#roleId").val(mRole.roleId),
					$("#roleNm1").val(mRole.roleNm),
					$("#roleDesc1").val(mRole.roleDesc),
					$("#school1").val(mRole.schId)
				}
			}
		})
	}
	
	 layui.use(['form', 'layedit', 'laydate'], function(){
	            var form = layui.form
	                ,layer = layui.layer
	                ,layedit = layui.layedit
	                ,laydate = layui.laydate;
	
	            form.render();
	            //创建一个编辑器
	            var editIndex = layedit.build('LAY_demo_editor');
	
	            form.verify({
	            	title: function(value){
	            		if(value.length < 2){
	            			return '角色名至少得2个字符';
	            		}
	            	}
	            	,roleDesc:function(value){
						if(value.length < 6){
							return '描述至少得6个字符';
						}
	            	}
	            	
	            });
	
	            //监听表单提交
	            form.on('submit(demo1)', function(data){
	              
	                    $.get({
							data:{
								roleNm: $("#roleNm").val(),
								roleDesc: $("#roleDesc").val(),
								schId: $("#school").val(),
								addUserId: 1
							}
							,url:path + "/authority/Role/insertRole"
							,success:function (res){
								if(res.code == 0){      //修改成功跳转到 院校信息页面
									//    layer.msg(res.Msg);
									//layer.closeAll();       //关闭确认提交的弹出
									
									window.location.reload();
		
								}else if(data.code == 1002){
									layer.msg(res.Msg);
								}
							}
						})
						 return false;
	                });
	
	               
	           
	
	            //表单取值
	            layui.$('#LAY-component-form-getval').on('click', function(){
	                var data = form.val('example');
	                alert(JSON.stringify(data));
	            });
	
	        });
	
	layui.use(['form', 'layedit', 'laydate'], function(){
	           var form = layui.form
			   ,layer = layui.layer
			   ,layedit = layui.layedit
			   ,laydate = layui.laydate;
		
	           form.render();
				
	           //创建一个编辑器
	           var editIndex = layedit.build('LAY_demo_editor');
				form.verify({
					title1: function(value){
						if(value.length < 2){
							return '角色名至少得2个字符';
						}
					}
					,roleDesc1:function(value){
						if(value.length < 6){
							return '描述至少得6个字符';
						}
					}
					
				});
	           
		
	           //监听表单提交
	           form.on('submit(demo2)', function(data){
				   
				   $.post({
					   url : path + "/authority/Role/updateRole"
					   ,data : {
							roleId: $("#roleId").val(),
							roleNm: $("#roleNm1").val(),
							roleDesc: $("#roleDesc1").val(),
							schId: $("#school1").val(),
							_method:'put'
					   }
					   ,success:function (res){
						   if(res.code == 0){      //修改成功跳转到 院校信息页面
								layer.msg(res.Msg);
							   layer.closeAll();       //关闭确认提交的弹出
							   
									window.location.reload();
		
						   }else if(data.code == 1002){
							   layer.msg(res.Msg);
						   }
					   }
				   })
				   return false;
					
			   });
		
	              
	          
		
	           //表单取值
	           layui.$('#LAY-component-form-getval').on('click', function(){
	               var data = form.val('example');
	               alert(JSON.stringify(data));
	           });
		
	       });	
	
  });
  </script>